<template>
  <sn-three-col-space :side-content="5" :content="14">
    <div slot="spaceContent" class="login">
      <el-link type="danger" :underline="false" class="login-title" @click="toHome">
        Future电影
      </el-link>
      <sn-row-has-two-col :left="16" :right="8">
        <el-image
            slot="one-col"
            src="http://s0.meituan.net/bs/file/?f=fe-sso-fs:build/page/static/banner/maoyan.png"/>
        <div slot="two-col" class="login-form">
          <sn-row-has-two-col :left="16" :right="8" class="login-method">
            <sn-text text="手机动态码登录" slot="one-col"/>
            <el-link type="danger" :underline="false" slot="two-col" @click="toLogin">
              账号登录
            </el-link>
          </sn-row-has-two-col>
          <el-form
              label-position="left"
              label-width="60px"
              :model="userInfo">
            <el-form-item label="手机号">
              <el-input v-model="userInfo.phoneNumber"></el-input>
            </el-form-item>
            <el-form-item label="验证码">
              <el-input v-model="userInfo.code"></el-input>
            </el-form-item>
            <el-button
                style="margin-left: 60px"
                @click="toSendMessage"
                type="info"
                class="margin-top-10">
              <sn-text :text="userInfo.sendMessage"/>
            </el-button>
            <el-button type="danger" style="width: 100%" @click="Login" class="margin-top-20">
              登录
            </el-button>
            <div class="to-register">
              还没有账号?
              <el-link
                  @click="toRegister"
                  type="danger" :underline="false">
                免费注册
              </el-link>
            </div>
          </el-form>
        </div>

      </sn-row-has-two-col>
    </div>
  </sn-three-col-space>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      userInfo: {
        countDown: 60,
        countDownInfo: ' 秒后再次发送',
        sendMessage: '免费获取短信动态码',
        code: '',
        phoneNumber: '',
      }
    }
  },
  methods: {
    toHome() {
      this.$router.push({name: 'home'})
    },
    toRegister() {
      this.$router.push({name: 'register'})
    },
    toLogin() {
      this.$router.push({name: 'login'})
    },
    toSendMessage() {
      this.$http({
        url: '/web/user/login/sms/sendCode',
        method: 'get',
        params: this.$http.adornParams({
          'phone': this.userInfo.phoneNumber
        })
      }).then(({data}) => {
        alert(data.msg)
      })
      const messageTimer = setInterval(() => {
        this.userInfo.countDown = this.userInfo.countDown - 1
        this.userInfo.sendMessage = this.userInfo.countDown + this.userInfo.countDownInfo
        if (this.userInfo.countDown === 0) {
          clearInterval(messageTimer)
          this.userInfo.countDown = 60
          this.userInfo.sendMessage = '免费获取短信动态码'
        }
      }, 1000)
    },
    Login() {
      this.$http({
        url: '/web/user/login/loginByPhone',
        method: 'post',
        data: this.$http.adornData({
          'phone': this.userInfo.phoneNumber,
          'code': this.userInfo.code
        })
      }).then(({data}) => {
        if (data.code === 0) {
          this.$store.commit(this.mutationsTypes.USER_LOGIN, data.data)
          this.$router.replace({
            name: 'home'
          })
        } else {
          this.myUtils.showMessage(this, data.msg)
        }
      })
    }
  },
}
</script>

<style scoped>
.login {
  margin-top: 50px;
}

.login-title {
  font-size: 50px;
  color: #ED1D24;
  font-weight: bold;
  margin-bottom: 30px;
}

.login-method {
  margin-bottom: 20px;
}

.to-register {
  margin-top: 20px;
}
</style>